package com.leejxx.health.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.leejxx.health.common.RedisConst;
import com.leejxx.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;

/**
 * @Author LeeJx
 * @ClassName SmsServiceImpl
 * @Description TODO
 * @date 2021/1/6 10:22
 * @Version 1.0
 */
@DubboService
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Autowired
    private RedisTemplate redisTemplate;

    private final IAcsClient acsClient = new DefaultAcsClient(DefaultProfile.getProfile("cn-hangzhou", "LTAI4G3BbmGHd1CnNFi24i7E", "Cv6Bbpgnc0h2l5gFN2QbNLfef9DWGy"));

    @Override
    public void sendValidateCode(String type, String telephone) {
        log.info("[短信-发送验证码]type:{},telephone:{}", type, telephone);
        //生成验证码(1000~9999)
        int code = (int) (1000 + new SecureRandom().nextDouble() * 8999);
        log.debug("[短信-发送验证码]code:{}", code);
        //保存到redis缓存
        String redisKey = RedisConst.VALIDATE_CODE_PREFIX + type + ":" + telephone;
//        redisTemplate.expire(redisKey, 5 * 60, TimeUnit.SECONDS);
//        redisTemplate.boundSetOps(redisKey).add(String.valueOf(code));
        redisTemplate.opsForValue().set(redisKey, String.valueOf(code), 5 * 60, TimeUnit.SECONDS);
        //构造请求参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", telephone);
        request.putQueryParameter("SignName", "面对疾风啦");
        request.putQueryParameter("TemplateCode", "SMS_208730226");
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            //发起请求
            CommonResponse response = acsClient.getCommonResponse(request);
            log.info("[短信-发送验证码]response:{}", response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean checkValidateCode(String type, String telephone, String code) {
        log.info("[短信-发送验证码]type:{},telephone:{},code:{}", type, telephone, code);
        String redisKey = RedisConst.VALIDATE_CODE_PREFIX + type + ":" + telephone;
        String expectVal = (String) redisTemplate.opsForValue().get(redisKey);
        if (!StringUtils.isEmpty(expectVal) && expectVal.equals(code)) {
            redisTemplate.delete(redisKey);
            return true;
        } else {
            return false;
        }
    }
}
